package com.cjjd.c.feature.mine.feedback;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarFragment;
import com.cjjd.c.bean.HotelQrCodeInfoBean;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.feature.dialog.TextDialog;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.DialogUtil;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.ToastUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by dada on 2017/12/23.
 */

public class FeedBackFragment extends ActionBarFragment {
    @Bind(R.id.ed_dec)
    EditText edDec;
    @Bind(R.id.et_email)
    EditText etEmail;
    @Bind(R.id.et_phone)
    EditText etPhone;

    @Override
    public Object getTitleString() {
        return "意见反馈";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_feedback;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mActionBar.getRightButton().setText("提交");
        mActionBar.getRightButton().setVisibility(View.VISIBLE);
    }

    @Override
    public void onRightButtonClick() {
        super.onRightButtonClick();


        String mobile=etPhone.getText().toString();
        String email=etEmail.getText().toString();
        String content=edDec.getText().toString();
        if(TextUtils.isEmpty(mobile)){
            ToastUtil.showToast("请输入手机号");
            return;
        }
        if(TextUtils.isEmpty(content)){
            ToastUtil.showToast("请输入内容");
            return;
        }

        DialogUtil.delDialog(mContext);
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("mobile",mobile);
        h1.put("email",email);
        h1.put("content",content);
        service.saveFeedBack(JsonUtils.convertParam("saveFeedBack", h1)).enqueue(new CallbackImpl<Object>() {
            @Override
            public void responseSuccess(Call<HttpResult<Object>> call, Response<HttpResult<Object>> response) {


                DialogUtil.dismissLoadingDialog(mContext);
                new TextDialog(mContext, "提交成功", new TextDialog.OnCloseListener() {
                    @Override
                    public void OnClick() {
                        mContext.finish();
                    }
                }).show();
            }

            @Override
            public void onFailure(Call<HttpResult<Object>> call, Throwable t) {
                super.onFailure(call, t);
                DialogUtil.dismissLoadingDialog(mContext);
            }

        });
    }


}
